﻿using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteDBDemo
{
    class LiteDBCache
    {
        private async void GetAutocompletes()
        {
            //using var db = new LiteDatabase("");
            //var col = db.GetCollection<Autocomplete>(nameof(Autocomplete));
            //var autocompletes = col.Find(o => o.Page == "AddOrder");
            //if (autocompletes?.Any() ?? false)
            //    _dicAutocomplete = autocompletes.ToDictionary(o => o.Code, o => o.Values);
            //await Task.CompletedTask;
        }
        private async void SetAutocomplete(string code, List<string> values)
        {
            //using var db = new LiteDatabase(BaseLiteDb.ConnStr);
            //var col = db.GetCollection<Autocomplete>(nameof(Autocomplete));
            //var curAutocomplete = col.FindOne(o => o.Page == "AddOrder" && o.Code == code);

            //var autocomplete = new Autocomplete()
            //{
            //    Page = "AddOrder",
            //    Code = code
            //};
            //if (curAutocomplete?.Values?.Any() ?? false)
            //{
            //    curAutocomplete.Values.Reverse();
            //    curAutocomplete.Values.AddRange(values);
            //    autocomplete.Values = curAutocomplete.Values.Take(5).ToList();
            //}
            //else
            //    autocomplete.Values = values;
            //var del = col.DeleteMany(o => o.Page == "AddOrder" && o.Code == code);
            //var ins = col.Insert(autocomplete);
            //await Task.CompletedTask;
        }
    }
    public class Autocomplete
    {
        public ObjectId Id { get; set; }
        /// <summary>
        /// 页面代码
        /// </summary>
        public string Page { get; set; }
        /// <summary>
        /// 具体代码
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 缓存记录
        /// </summary>
        public List<string> Values { get; set; }
    }
}
